﻿using System;
using System.IO;
using System.Data;
using System.Windows;
using System.Collections;
using System.Windows.Input;
using System.ComponentModel;
using System.Windows.Controls;
using Sinacor.Infra.UI.Common;
using Sinacor.Infra.UI.Controls;
using Sinacor.Infra.UI.Controls.SmartClient;
using System.Collections.Generic;
using Sinacor.Infra.UI.Controls.SmartClient.Validation;
using Sinacor.UI.Bovespa.Contabil.Presenter;


namespace Sinacor.UI.Bovespa.Contabil.View.SmartClient
{
    public partial class GeracaoIntegracaoContabil : SinacorWindow, IGeracaoIntegracaoContabil
    {
        #region Propriedades

        public Presenter.GeracaoIntegracaoContabil _presenter { get; set; }

        #endregion

        #region Construtor

        public GeracaoIntegracaoContabil()
        {
            InitializeComponent();

            _presenter = new Sinacor.UI.Bovespa.Contabil.Presenter.GeracaoIntegracaoContabil(this);
            this.DataContext = this._presenter;

            this.AfterInitialize += new RoutedEventHandler(GeracaoIntegracaoContabil_AfterInitialize);
        }

        #endregion

        #region Metodos

        /// <summary>
        /// Gera integração contábil
        /// </summary>
        private void GerarIntegracaoContabil()
        {
            _presenter.GerarIntegracaoContabil();
        }

        #endregion

        #region Eventos

        /// <summary>
        /// Evento Click para execução do processo
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <param name="clickCount"></param>
        private void BtnOk_OnBufferedClick(object sender, RoutedEventArgs e, int clickCount)
        {
            this.ShowUserWaitingProcess();
            this.BtnOk.IsEnabled = false;

            this.GerarIntegracaoContabil();

            this.BtnOk.IsEnabled = true;
            this.HideUserWaitingProcess();
        }

        /// <summary>
        /// Evento AfterInitialize
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GeracaoIntegracaoContabil_AfterInitialize(object sender, RoutedEventArgs e)
        {
            _presenter.Inicializar();
        }

        #endregion

        #region IGeracaoIntegracaoContabil Members

        /// <summary>
        /// Atualiza calendario
        /// </summary>
        public void AtualizarCalendario()
        {
            this.CboDatasDisponiveis.UpdateTarget(SinacorComboBox.ItemsSourceProperty);
        }

        /// <summary>
        /// Informa sucesso na operação
        /// </summary>
        public void InformarSucessoOperacao()
        {
            SinacorMessageBox.Show(this.TryFindResource("Bovespa.Contabil.Message.OperacaoRealizada").ToString(), SinacorMessageBoxButton.Ok, SinacorMessageBoxImage.Information);
        }

        /// <summary>
        /// Seleciona ultima data no calendário
        /// </summary>
        public void SelecionarUltimaDataCalendario()
        {
            if (_presenter.ListaCalendarios != null && _presenter.ListaCalendarios.Count > 0)
            {
                _presenter.Filtro = new Sinacor.UI.Bovespa.Contabil.Model.Parameter.FiltroIntegracaoContabil
                {
                    DataMovimento = _presenter.ListaCalendarios[0].DataPregao
                };

                this.CboDatasDisponiveis.UpdateTarget(SinacorComboBox.SelectedValueProperty);
            }
        }

        #endregion
    }
}